<?php
require_once(SITE_ROOT . 'class/db/DB.class.php');
require_once(SITE_ROOT . 'class/bean/Alliance.class.php');

class DBAlliance {
	
	public function getAlliances($orderby = null) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($orderby == null) {
			$select = "SELECT `alliance#`, `name` FROM `talliance` ORDER BY `name`";
		}
		else $select = "SELECT `alliance#`, `name` FROM `talliance` ORDER BY `name`";
		
		if ($stmt = $connection->prepare($select)) {
			$stmt->execute();
			$stmt->bind_result($allianceID, $name);
			
			$alliances = new ArrayObject();
			
			while ($stmt->fetch()) {
				$alliance = new Alliance($allianceID, $name);
				$alliances->append($alliance);
			}			
			$stmt->close();
			$db->disconnect();
			
			return $alliances;
		}
	}
	
	public function saveAlliance($alliance) {
		$db = new DB;
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('INSERT INTO `talliance` (`alliance#`, `name`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `name`=VALUES(`name`)')) {
			$stmt->bind_param('is', $allianceID, $name);
				
			$allianceID = $alliance->getAllianceID();
			$name = $alliance->getName();
			
			$stmt->execute();
			
			$stmt->close();
			$db->disconnect();
		}
	}
		
}

?>